package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Print;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.PrintMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private PrintMapper printMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //查询购物车中是否已经存在商品项
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取当前用户id，只能查询自己的购物车
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //如果已经存在普通商品，则商品项数量加1
        if (list != null && list.size() == 1) {
            shoppingCart = list.get(0);
            //排除打印
            if (shoppingCart.getPrintId() == null) {
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }

        } else {
            //如果不存在，插入一条商品项数据到购物车
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            Long printId = shoppingCartDTO.getPrintId();
            if (dishId != null) {
                //插入一条菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            } else if (setmealId != null) {
                //插入套餐
                Setmeal setmeal = setmealMapper.findById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            } else {
                //插入一条打印
                Print print = printMapper.getById(printId);
                shoppingCart.setName(print.getName());
                shoppingCart.setImage(print.getImage());
                shoppingCart.setAmount(print.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(userId);

            shoppingCartMapper.insert(shoppingCart);
        }


    }

    /**
     * 查询购物车
     *
     * @return
     */
    public List<ShoppingCart> list() {

        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车
     */
    public void clean() {
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clearCart(userId);
    }

    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     */
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取当前用户id，只能删除自己的购物车
        shoppingCart.setUserId(userId);

        //判断商品是一个还是多个
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null) {
            if (list.get(0).getNumber() == 1) {
                //购物项只有一个,删除此项
                shoppingCartMapper.delete(shoppingCart);
            } else {
                //购物项有多个，直接数量减一
                ShoppingCart shoppingCart1 = list.get(0);
                shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
                shoppingCartMapper.updateNumberById(shoppingCart1);
            }
        }


    }
}
